package engine.gui.elements;

import engine.gui.Action;
import engine.gui.Component;
import engine.gui.GUIStyle;

public class Button extends Component{
	
	private String text;
	private Image image;
	
	private boolean hover = false;
	private Action action;
	
	private boolean mouseOverAndDown = false; 		//this variable will be set to true, if the mouse is currently pressed and over the button
	
	public Button() {
	}
	
	public Button(String text) {
		this();
		this.text = text;
	}
	
	public Button(String text, int posX, int posY,
			int width, int height) {
		this(text);
		setPosX(posX);
		setPosY(posY);
		setWidth(width);
		setHeight(height);
	}

	
	@Override
	public void render(int x, int y, int width, int height) {
		GUIStyle style = getCurrentStyle();
		
		renderButton(style, x, y, width, height);
		renderText(style, x, y, width, height);
	}
	
	@Override
	public void reset() {
		hover = false;
	}
	
	private void renderButton(GUIStyle style, int x, int y, int width, int height) {
		if(mouseOverAndDown) {
			//render active image
			getWindowManager().getGuiShader().sendUniform4f("color", style.getButtonActivatedColor());
			
			if(style.getButtonActivateImage() != null) {
				style.getButtonActivateImage().draw(x + getPosX(), y + getPosY(), getWidth(), getHeight());
			}
		} else if(hover) {
			//render hover image
			getWindowManager().getGuiShader().sendUniform4f("color", style.getButtonHoverColor());
			
			if(style.getButtonHoverImage() != null) {
				style.getButtonHoverImage().draw(x + getPosX(), y + getPosY(), getWidth(), getHeight());
			}
		} else {
			//render usual button
			getWindowManager().getGuiShader().sendUniform4f("color", style.getButtonStandardColor());
			
			if(style.getButtonBackgroundImage() != null) {
				style.getButtonBackgroundImage().draw(x + getPosX(), y + getPosY(), getWidth(), getHeight());
			}
		}
	}
	
	private void renderText(GUIStyle style, int x, int y, int width, int height) {
		if(text != null) {
			float h = style.getFont().getHeight();
			float w = style.getFont().getWidth(text);
			
			if(mouseOverAndDown){
				getWindowManager().getGuiShader().sendUniform4f("color", style.getButtonTextActiveColor());
			} else if(hover) {
				getWindowManager().getGuiShader().sendUniform4f("color", style.getButtonTextHoverColor());
			} else {
				getWindowManager().getGuiShader().sendUniform4f("color", style.getButtonTextColor());
			}
			
			style.getFont().draw(text, x + getPosX() + (getWidth() - w) / 2, y + getPosY() + (getHeight() + h) / 2);
			
			if(getImage() != null) {
				getWindowManager().getGuiShader().sendUniform4f("color", 1, 1, 1, 1);
				getImage().draw(x + getPosX(), y + getPosY(), getWidth(), getHeight());
			}
		}
	}
	
	@Override
	public boolean mousePressed(int posX, int posY, int button) {
		if(button == 0 && posX > getPosX() && posX < getPosX() + getWidth() && posY > getPosY() && posY < getPosY() + getHeight()) {
			mouseOverAndDown = true;
			return true;
		}
		
		return false;
	}
	
	@Override
	public boolean mouseReleased(int posX, int posY, int button) {
		if(mouseOverAndDown && button == 0 && posX > getPosX() && posX < getPosX() + getWidth() && posY > getPosY() && posY < getPosY() + getHeight()) {
			if(action != null) {
				action.performAction();
			}
			mouseOverAndDown = false;
			return true;
		}
		
		return false;
	}
	
	@Override
	public boolean mouseMoved(int posX, int posY, int button) {
		hover = (posX > getPosX() && posX < getPosX() + getWidth() && posY > getPosY() && posY < getPosY() + getHeight());
		
		return hover;
	}
	
	@Override
	public boolean mouseDragged(int posX, int posY, int button) {
		if(mouseOverAndDown) {
			mouseOverAndDown = (posX > getPosX() && posX < getPosX() + getWidth() && posY > getPosY() && posY < getPosY() + getHeight());
			return mouseOverAndDown;
		}
		
		return false;
	}

	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
	}
	
	@Override
	public void activator() {
		if(action != null) {
			action.performAction();
		}
	}
	
	public void setImage(Image image) {
		this.image = image;
	}
	
	public Image getImage() {
		return image;
	}
}